package com.example.demo.demos.controller;

import com.example.demo.demos.entity.User;
import com.example.demo.demos.mapper.UserMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
//@CrossOrigin
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String Hello(){
        return "hello";
    }


    @RequestMapping(value = "/query",method = RequestMethod.GET)
    public List query(){
        List<User> list = userMapper.queryAll();
        System.out.println(list);
        return list;
    }


    @RequestMapping(value = "/queryByName",method = RequestMethod.GET)
    public List querybyname(String username){
        List<User> list = userMapper.queryByName(username);
        System.out.println(list);
        return list;
    }


    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public String insert(User user){

        int insert = userMapper.insert(user);
        if(insert>0){
            return "insert success";
        }else {
            return "insert fail";
        }
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public String delete(int id){
        int delete = userMapper.delete(id);
        if(delete>0){
            return "delete success";
        }else {
            return "delete fail";}
    }
}
